import { CCBoolean, Component, EventTouch, NodeEventType, UITransform, _decorator, v3 } from "cc";
const { ccclass, property } = _decorator;
@ccclass
export default class MoveControl extends Component {
    @property(Component)
    /**
     * 被拖拽的节点
     */
    private content: Component = null;
    //@property(CCBoolean)

    //TOUCH_START计数
    private touchStartCount: number = 0;
    //TOUCH_END计数
    private touchEndCount: number = 0;
    //触点与目标的x轴距离
    private touchX: number = 0;
    //触点与目标的y轴距离
    private touchY: number = 0;
    protected onLoad(): void {
        //
    }
    protected onEnable(): void {
        this.node.on(NodeEventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(NodeEventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(NodeEventType.TOUCH_END, this.onTouchEnd, this);
    }
    protected onDisable(): void {
        this.node.off(NodeEventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(NodeEventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.off(NodeEventType.TOUCH_END, this.onTouchEnd, this);
    }
    protected onTouchStart(event:EventTouch): void {
        const p=event.touch.getUILocation()
        const target=this.content.node.getWorldPosition()
        this.touchX=p.x-target.x
        this.touchY=p.y-target.y
        this.touchStartCount++
        /*if(this.touchStartCount>2){
            this.touchStartCount=0
        }*/
        console.log(this.touchStartCount,this.touchEndCount)
    }
    protected onTouchMove(event:EventTouch): void {
        if(this.touchStartCount!=1)return
        const touchs = event.getTouches();
        if(touchs.length!=1)return
        const p=event.touch.getUILocation()
        this.content.node.setWorldPosition(v3(p.x-this.touchX,p.y-this.touchY,0))
        //this.node.dispatchEvent(event)
    }
    protected onTouchEnd(event:EventTouch): void {
        //this.touchStartCount--
        this.touchEndCount++
        if((this.touchStartCount==this.touchEndCount)||(this.touchStartCount>=2)){
            this.touchEndCount=0
            this.touchStartCount=0
        }
    }
}